<template>
  <div class="app-container">
    <el-col :span="4">
      <el-input
        placeholder="请输入部门名称"
        prefix-icon="el-icon-search"
        size="small"
        v-model="filterText">
      </el-input>
      <el-tree
        class="filter-tree"
        :data="data"
        :props="defaultProps"
        default-expand-all
        @node-click="fetchNode"
        :filter-node-method="filterNode"
        ref="tree">
      </el-tree>
    </el-col>
    <el-col :span="20">
      <!-- 搜索区 -->
      <el-row>
        <el-form v-hasPermi ref="queryForm" :model="queryParams" size="small" :inline="true">
          <el-form-item label="用户名称" prop="name">
            <el-input
              placeholder="请输入用户名称"
              v-model="queryParams.name"
              clearable
            >
            </el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              placeholder="请输入手机号码"
              v-model="queryParams.phone"
              clearable
            >
            </el-input>
          </el-form-item>
          <el-form-item label="用户状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="状态"
              clearable>
              <el-option
                v-for="dict in statusList"
                :label="dict.dictLabel"
                :value="dict.dictValue"/>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="timeData"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <!--功能区-->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd()"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate()"
            :disabled="single"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete()"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
          >导出</el-button>
        </el-col>
      </el-row>
      <!-- 数据展示区table-->
      <el-table v-loading="loading" :data="pageData.rows" @selection-change="handleSelectionChange">
        <el-table-column type="selection"/>
        <el-table-column label="用户编号" prop="id" width="100" />
        <el-table-column label="用户昵称" prop="name" />
        <el-table-column label="手机号码" prop="phone"  />
        <el-table-column label="部门名称" prop="deptName"  />
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==='0'">正常</el-tag>
            <el-tag v-else type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页条 -->
      <div style="margin-top: 10px;">
        <el-pagination
          v-if="pageData.total>0"
          :current-page="queryParams.page"
          :page-sizes="[10, 20, 30, 40,50,100]"
          :page-size="queryParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-col>
    <!-- 新增和修改对话框-->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-col :span="12" >
            <el-form-item label="用户昵称" prop="name">
              <el-input v-model="form.name" placeholder="请输入用户昵称" />
            </el-form-item>
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号码" />
            </el-form-item>
            <el-form-item label="用户密码"  prop="password">
              <el-input placeholder="请输入密码" v-model="form.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-select
                v-model="form.sex"
                placeholder="性别"
                clearable>
                <el-option
                  v-for="dict in sexList"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptName">
              <el-select
                v-model="form.deptId"
                placeholder="选择归属部门"
                clearable>
                <el-option
                  v-for="dict in deptNameList"
                  :label="dict.name"
                  :value="dict.id"/>
              </el-select>
            </el-form-item>
            <el-form-item label="岗位" prop="postName">
              <el-select
                v-model="form.postId"
                placeholder="选择岗位"
                clearable>
                <el-option
                  v-for="dict in postNameList"
                  :label="dict.name"
                  :value="dict.id"/>
              </el-select>
            </el-form-item>
            <el-form-item label="角色" prop="roleName">
              <el-select
                v-model="form.roleId"
                placeholder="角色"
                clearable>
                <el-option
                  v-for="dict in roleList"
                  :label="dict.name"
                  :value="dict.id"/>
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
            <el-select
              v-model="form.status"
              placeholder="状态"
              clearable>
              <el-option
                  v-for="dict in statusList"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
            </el-select>
            </el-form-item>
          </el-col>
        <el-col>
          <el-form-item label="请输入内容">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.remark">
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading1" @click="submitForm">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { treeselect } from '@/api/system/dept'
import pageMixin from '/src/utils/mixin/psgeMixin.js'
import addorupdateMixin from '/src/utils/mixin/addorupdateMixin.js'
import { getDictTypeList } from '../../../api/system/dictData'
import {getList, addData, addorUpdate, findUser, findTreeUser} from '../../../api/system/user'
export default {
  mixins: [pageMixin, addorupdateMixin],
  name: 'user',
  data() {
    return {
      filterText: '',
      data: [],
      deptNameList: '',
      postNameList: '',
      roleList: '',
      sexList: '',
      timeData: '',
      statusList: '',
      loading1: false,
      defaultProps: {
        children: 'children',
        label: 'deptName'
      },
      rules: {
        name: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ]
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    // 新增或修改
    afterSubmitFormHandler() {
      this.loading1=true
      addorUpdate(this.form).then(resp => {
        this.fetchData()
        this.loading1=false
        this.open=false
      })
    },
    // 修改之前查数据
    beforeHandlerUpdate(id) {
      findUser({id:id}).then(resp => {
        this.form=resp;
        this.form.deptId=resp.deptId.toString()
        this.form.roleId=resp.roleId.toString()
        this.form.postId=resp.postId.toString()
        console.log(this.form)
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.deptName.indexOf(value) !== -1;
    },
    // 查用户
    fetchData() {
      this.loading = true
      // 删除 日期
      delete this.queryParams.beginTime
      delete this.queryParams.endTime
      if (this.timeData && this.timeData.length === 2) {
        this.queryParams.startTime = this.timeData[0]
        this.queryParams.endTime = this.timeData[1]
      }
      getList(this.queryParams).then(resp => {
        this.pageData.rows = resp
        this.loading = false
      })
      treeselect(this.queryParams).then(response => {
        this.data = response
        this.loading = false
      })
    },
    // 点击节点查用户
    fetchNode(data) {
      this.loading = true
     findTreeUser({id:data.id}).then(resp => {
       this.pageData.rows = resp
       this.loading = false
     })
    },
    // 状态数据
    fetchCommonData() {
      getDictTypeList('sys_normal_disable').then(resp=> {
        this.statusList= resp
      })
      getDictTypeList('sys_user_sex').then(resp=> {
        this.sexList= resp
      })
      addData().then(resp => {
        this.deptNameList=resp.deptName
        this.roleList=resp.roleName
        this.postNameList=resp.postName
      })
    },
    handleAvatarSuccess(res, file) {
      this.form.picture = res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
  },
  created() {
    this.fetchCommonData()
  }

};
</script>
<style>
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.avatar {
  width: 50px;
  height: 50px;
  display: block;
}
</style>
